/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

import beanInfo.users;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Quynh Nhu
 */
public class checkValidate {

    public static boolean isUsername(String username, int min, int max) {
        return isMatch("^[a-zA-Z][a-zA-Z0-9_]{" + (min - 1) + "," + (max - 1) + "}$", username);
    }

    public static boolean isPassword(String password, int min, int max) {
        return isMatch("[\\p{ASCII}]{" + min + "," + max + "}$", password);
    }

    public static boolean isEmail(String email) {
        return isMatch("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", email);
    }

    public static boolean isIp(String str) {
        return isMatch("^(\\d{1}|\\d{2}|[0-1]\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d{1}|\\d{2}|[0-1]\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d{1}|\\d{2}|[0-1]\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d{1}|\\d{2}|[0-1]\\d{2}|2[0-4]\\d|25[0-5])$", str);
    }

    public static boolean isUrl(String str) {
        return isMatch("^(file|http|https|ftp|mms|telnet|news|wais|mailto):\\/\\/(.+)$", str);
    }

    public static boolean isMobile(String str) {
        return isMatch("^0{0,1}1(3|5)[0-9]{9}$", str);
    }

    public static boolean isPhone(String str) {
        return isMatch("(^[0-9]{3,4}\\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\\([0-9]{3,4}\\)[0-9]{3,8}$)|(^[0-9]{3,4}[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)", str);
    }

    public static boolean isPostcode(String str) {
        return isMatch("^(\\d){6}$", str);
    }

    public static boolean isMatch(String regex, String data) {
        return data == null ? false : Pattern.compile(regex).matcher(data).find();
    }

    public static boolean validateFirstName(String firstName) {
        return firstName.matches("[A-Z][a-zA-Z]*");
    }

    public static boolean validateLastName(String lastName) {
        return lastName.matches("[a-zA-z]+([ '-][a-zA-Z]+)*");
    }

    public static boolean IsWhiteSpace(String space) {
        Pattern pt = Pattern.compile("[^\\s]");
        Matcher mc = pt.matcher(space);
        if (!mc.find()) {
            return false;
        }
        return true;
    }

    public static boolean checkStringLength(String str, int min, int max) {

        if (str.length() == 0) {
            return false;
        } else if (str.length() < min) {
            return false;
        } else if (str.length() > max) {
            return false;
        }
        return true;

    }

    public static boolean checkIntNumber(String num) {
        try {
            Integer.parseInt(num);
        } catch (NumberFormatException ex) {
            return false;
        }
        return true;
    }

    public static boolean checkFloatNumber(String num) {
        try {
            Float.parseFloat(num);
        } catch (NumberFormatException ex) {
            return false;
        }
        return true;
    }

    public static int compareDateTime(Date d1, Date d2) {
        int ok;
        if (d1.equals(d2)) {
            ok = 1; // bang
        } else if (d1.before(d2)) {
            ok = 2; // truoc
        } else {
            ok = 3; // sau
        }
        return ok;

    }
    
    public static boolean compareTwoString(String s1, String s2)
    {
        if(s1.compareToIgnoreCase(s2)==0)
           return true;
        else
            return false;
    }
     public static boolean checkPhone(String n) {
        if (n == null || n.length() > 15 || n.length() < 9) {
            return false;
        } else {
            String strPattern = "[^0-9]";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return !m.find();
        }
    }
      public static boolean checkNumber(String n) {
        if (n == null || n.length() > 10  || n.length() < 1) {
            return false;
        } else {
            String strPattern = "[^0-9]";
            Pattern p;
            Matcher m;
            int flag = Pattern.CASE_INSENSITIVE;
            p = Pattern.compile(strPattern, flag);
            m = p.matcher(n);
            return !m.find();
        }
    }
      public static boolean checkDateIn(String n) {
        if (n == null || n.length() < 10) {
            return false;
        } else {
            boolean flag = true;
            String dd, mm, yyyy;
            int d, m, y;
            yyyy = n.substring(0, 4);
            mm = n.substring(5, 7);
            dd = n.substring(8, 10);
            try {
                Date dat = new Date();
                Calendar date = Calendar.getInstance();
                date.setTime(dat);
                int y1 = date.get(Calendar.YEAR);
                int m1 = date.get(Calendar.MONTH);
                int d1 = date.get(Calendar.DAY_OF_MONTH);
                m = Integer.parseInt(mm);
                d = Integer.parseInt(dd);
                y = Integer.parseInt(yyyy);
                if ((d < 1) && (d > 31)) {
                    flag = false;
                }
                if ((m < 1) && (m > 12)) {
                    flag = false;
                }
                if (y < 1) {
                    flag = false;
                }
                if (y1 < y) {
                    flag = false;
                }
            } catch (Exception e) {
                flag = false;
            }
            return flag;
        }
      }
      public void checkUser()
      {
          users ud=new users();
          
      }
     
}
